package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;


    /*
     * 根据日期区间来查询营业额
     * */
    public TurnoverReportVO getTurnoverReportVO(LocalDate begin, LocalDate end) {

        List<LocalDate> dateList = new ArrayList<>();

        while (begin != end) {
            dateList.add(begin);
            begin.plusDays(1);
        }
        dateList.add(end);


        List<Double> turnOverList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //select sum(amount) from orders where order_time>? and order_time<? and status=5
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            map.put("status", Orders.COMPLETED);

            Double turnOver = orderMapper.getSumByMap(map);
            turnOver = turnOver == null ? 0.0 : turnOver;
            turnOverList.add(turnOver);
        }


        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnOverList, ","))
                .build();
    }

    /*
     * 用户数据统计
     * */
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();

        while (begin != end) {
            dateList.add(begin);
            begin.plusDays(1);
        }
        dateList.add(end);

        List<Integer> newUserList1 = new ArrayList<>();//select count(id) where create_time <? and create_time>?
        List<Integer> totalUserList = new ArrayList<>();//select count(id) where create_time<?

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();

            map.put("end", endTime);

            Integer total = userMapper.countByMap(map);//总的
            totalUserList.add(total);
            map.put("begin", beginTime);
            Integer newUser = userMapper.countByMap(map);
            newUserList1.add(newUser);
        }


        UserReportVO userReportVO = new UserReportVO();
        userReportVO.setDateList(StringUtils.join(dateList, ","));
        userReportVO.setNewUserList(StringUtils.join(newUserList1, ","));
        userReportVO.setNewUserList(StringUtils.join(totalUserList, ","));


        return userReportVO;

    }

    /*
     * 订单数据统计
     * */
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();

        while (begin != end) {
            dateList.add(begin);
            begin.plusDays(1);
        }
        dateList.add(end);

        Integer all = 0;
        Integer ValidAll = 0;

        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validCountList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            Integer total = orderMapper.countByMap(map);//总的
            map.put("status", Orders.COMPLETED);
            Integer use = orderMapper.countByMap(map);//有效的

            orderCountList.add(total);
            validCountList.add(use);
            all += total;
            ValidAll += use;
        }

        double a = 0.0;
        if (all != 0) {
            a = all.doubleValue() / ValidAll;
        }

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validCountList, ","))
                .totalOrderCount(all)
                .validOrderCount(ValidAll)
                .orderCompletionRate(a)
                .build();
    }

    /*
     * 销量前十统计
     * */
    public SalesTop10ReportVO getTop10Statistics(LocalDate begin, LocalDate end) {

        //select od,name,sum(od.number) from order_detail od ,orders o where od.order_id =o.id and
        // o.status=5 and  o.order_time> ?and o.order_time<? group by od.name order by number desc
        // limit 0,10


        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        List<GoodsSalesDTO> salesTop = orderMapper.getSalesTop(beginTime, endTime);

        List<String> names = salesTop.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");


        List<Integer> numbers = salesTop.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(numbers, ",");

        return SalesTop10ReportVO.builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }
}
